using UnityEngine;
using System.Collections;

public class Inventory {
	private ArrayList entities = new ArrayList();
	private int position = 0;
	
	// Removes and returns the selected Entity.
	public Entity pop() {
		if (entities.Count > 0) {
			Entity result = (Entity) entities[position];
			entities.RemoveAt(position);
			
			if (entities.Count > 0) {
				position %= entities.Count;
			} else {
				position = 0;
			}
			
			return result;
		} else {
			return null;	
		}
	}
	
	// Returns the selected Entity without removing it.
	public Entity peek() {
		if (entities.Count > 0) {
			return (Entity) entities[position];
		} else {
			return null;	
		}
	}
	
	// Add an Entity to the inventory.
	public void push(Entity entity) {
		entities.Add(entity);	
	}
	
	// Returns the number of Entities in the inventory.
	public int count() {
		return entities.Count;	
	}
	
	// Shifts the selection left or right. Use the public enum 
	// Direction to choose direction. This function makes sure
	// the selection does not go out of bounds. If the number
	// of items is 0, the selection will be 0 but the peek()
	// function will return null.
	public enum Direction {LEFT, RIGHT};
	public void shift(Direction dir) {
		switch (dir) {
			case Direction.LEFT:  shiftLeft(); break;
			case Direction.RIGHT: shiftRight(); break;
		}
	}
	
	// Selects the Item to the left of the current selection.
	private void shiftLeft() {
		if (entities.Count > 0) {
			position--;
			if (position < 0) {
				position = entities.Count - 1;
			}
		} else {
			position = 0;	
		}
	}
	
	// Selects the Item to the right of the current selection.
	private void shiftRight() {
		if (entities.Count > 0) {
			position = (position + 1) % entities.Count;
		} else {
			position = 0;	
		}
	}
}
